# -----------------------------------------------------------------
# Licensed Materials - Property of IBM
#
# 5725-M82 - IBM Presence Zones
#
# (c) Copyright IBM Corp. 2013, 2014
#
# All rights reserved.
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
# -----------------------------------------------------------------

# Arguments in the following format:
# param1: 1.11.111.1~2.22.222.2
# param2: add/remove/replace add means add app node to plugin; remove means remove app node from plugin; replace means remove all old server and add all new server;
# param3: true/false  used to mark if need to enable SSL when configure plugin -- just work for add action

IHS_DIR=/opt/IBM/HTTPServer

# Process command line arguments
args=($@)
   
IFS='~' read -a appserver_arr <<< "${args[0]}"

echo "Action: ${args[1]}"

echo "Should enable SSL? ${args[2]}"

#Start IHS Server
#echo "Stop IHS Server ......"
#sudo /opt/IBM/HTTPServer/bin/apachectl stop

if [ ${args[1]} == "add" ] || [ ${args[1]} == "replace" ]
then 
   if [ ${args[2]} == true ]
   then
     plugin_cfg_template=serverplugin_cfg_xml
   else
     plugin_cfg_template=serverplugin_cfg_no_ssl_xml
   fi
fi

echo "the finial plugin template is: $plugin_cfg_template"

if [ ${args[1]} == "replace" ]
then
   echo "Remove old server configure in IHS plugin configure file "
   `\cp /scripts/plugin-cfg.xml.template $IHS_DIR/conf/plugin-cfg.xml`
fi

if [ ${#appserver_arr[@]} != 0 ]
then 
   for i in "${!appserver_arr[@]}"; do 
      appserver_ip=${appserver_arr[$i]}
      
      #remove old configure template whatever if it is exist
      `rm -rf ./tmp/serverplugin_cfg_$appserver_ip.txt`
      
      if [ ${args[1]} == "add" ] || [ ${args[1]} == "replace" ]
      then
        echo "Generate server configure for $appserver_ip ......"
        sed 's/appserver_ip/'"$appserver_ip"'/g' $plugin_cfg_template.template >  ./tmp/serverplugin_cfg_$appserver_ip.txt
      fi
      
      
      grep "$appserver_ip" $IHS_DIR/conf/plugin-cfg.xml
   
      if [ $? == 0 ]
        then
         linenumStart=`grep -n "<!--start $appserver_ip-->" $IHS_DIR/conf/plugin-cfg.xml | cut -d: -f1`
         linenumEnd=`grep -n "<!--end $appserver_ip-->" $IHS_DIR/conf/plugin-cfg.xml | cut -d: -f1`
         echo "Delete old configure from line: $linenumStart to line $linenumEnd"
         `sed -i "$linenumStart","$linenumEnd"d $IHS_DIR/conf/plugin-cfg.xml`
      fi
  
      
      if [ ${args[1]} == "add" ] || [ ${args[1]} == "replace" ]
      then
         echo "Insert server configure for $appserver_ip ......"
         linenum=`grep -n "Point to configure serverCluster" $IHS_DIR/conf/plugin-cfg.xml | cut -d: -f1`
         contentpath=./tmp/serverplugin_cfg_$appserver_ip.txt
         `sed -i ''$linenum' r '$contentpath'' $IHS_DIR/conf/plugin-cfg.xml`
      fi
   done
else
   echo "appserver_autho_arr array is empty"
fi


#Start IHS Server
#echo "Start IHS Server ......"
#sudo /opt/IBM/HTTPServer/bin/apachectl start